<?php

session_start();
require_once '../src/MyDBi.php';
$db = new MysqliDb();



$_orderBy = "IdRoom";

$monthSelected = (strlen($_POST["month"] == 1)) ? "0" . $_POST["month"] : $_POST["month"];
$yearSelected = $_POST["year"];


$result = $db->get ('rooms');



//$query = $con->selectFrom("rooms", $columns = null, $where = null, $like = false, $orderby = null, $direction = "DESC", $limit = null, $offset = null);

$days_in_month = cal_days_in_month(CAL_GREGORIAN, $monthSelected, $yearSelected);


//$result = $query["result"];
if ($db->count > 0) {

    echo '';

    //echo '<table id="resultadosInner">';
    //echo "<thead ><tr><th colspan='" . $days_in_month . "'>Ocupación</th></tr> </thead >";
    echo "<thead ><tr>";
    for ($i = 0; $i <= $days_in_month; $i++) {
        if ($i == 0) {
            echo "<th style='' width='200'><div style='width: 120px;'>Habitación<br>Cama</div></th>";
        } else {
//            $dw = date("w", mktime(0, 0, 0, $monthSelected, $i, $yearSelected));
            $dw = date("D", mktime(0, 0, 0, $monthSelected, $i, $yearSelected));

            if (date('Ymd') == date('Ymd', mktime(0, 0, 0, $monthSelected, $i, $yearSelected))) {
                echo "<th class='currentDay'>" . $dw . " " . $i . "</th>";
            } else {
                echo "<th>" . $dw . " " . $i . "</th>";
            }
        }
    }
    $altura = 25;
    $contador = 0;
    echo "</tr></thead>";
    foreach ($result as $row) {

        echo "<tr ><td style='white-space: nowrap;color:white;'>" . $row["Nombre"] . "</td>";
        // Agrego las cabeceras, las habitaciones
        for ($i = 1; $i <= $days_in_month; $i++) {
            echo "<td><input "
            . "type='button' onclick='getDetail(this)' name ='" . $yearSelected . "-" . $monthSelected . "-" . $i . "-" . $row["IdRoom"] . "-" . $row["Lugares"] . "-H' ></input></td>";
        }
        echo "</tr>";

        $altura = $altura + 42.75 + $contador * 0.29;
//        $altura = $altura + 42.75 + $contador * 0.29;
        // Agrego las camas

        for ($x = 1; $x <= $row["Lugares"]; $x++) {

            echo "<tr><td style='background-color:#39b1cc;opacity:0.5;height:20px; color:white;'>"
            . "" . $x . ""
            . "</td>";
            $altura = $altura + 25;
            for ($i = 1; $i <= $days_in_month; $i++) {
                $color = "wheat";

                // Dentro de la celda, hay un botón que siempre llama a getDetails, lo que va cambiando es el value del botón para indicar
//                el menu que se tiene que mostrar
                echo "<td "
                . "style='position:absolute; top: " . ($altura) . "px; left: " . (64 * $i + 62) . "px; background-color:" . $color . ";height:20px;' onmouseover='changeBack(this);'>"
                . "<input style='height:20px;color:wheat;' "
                . "type='button' onclick='getDetail(this)' name ='" . $yearSelected . "-" . $monthSelected . "-" . $i . "-" . $row["IdRoom"] . "-" . $x . "-C' "
                . "id ='" . $yearSelected . "-" . $monthSelected . "-" . $i . "-" . $row["IdRoom"] . "-" . $x . "-C' value='V'></input>"
                . "<label id='" . $yearSelected . "-" . $monthSelected . "-" . $i . "-" . $row["IdRoom"] . "-" . $x . "-L' "
                . "style='font-family:tahoma; font-size: 0.9em; white-space: nowrap;z-index:2;height:20px;color:grey;position:absolute; top: 2px; left: 5px;'"
                . " onclick=\"fireOnclick('" . $yearSelected . "-" . $monthSelected . "-" . $i . "-" . $row["IdRoom"] . "-" . $x . "-C')\">"
                . "</label> "
                . "</td>";
            }
            echo "</tr>";
        }
        $contador = $contador + 1;
    }
    //echo '</table>';
    echo '';
}

?>